package org.netwia.networks;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.netwia.utils.DomainUtils;
import org.netwia.utils.StartStopService;
import org.w3c.dom.Document;

/**
 * Test application for the network summary.
 * Starts server and master client and allows user to enter inputs + exit.
 * 
 * @author radek.hecl
 *
 */
public class NetworkSummaryTestApplication {

    /**
     * Port for the embedded test server.
     */
    private static final int SERVER_PORT = 43239;

    /**
     * Prevent creating new instances from outside.
     */
    private NetworkSummaryTestApplication() {
    }
    
    /**
     * Main method.
     * 
     * @param args command line arguments
     */
    public static void main(String[] args) {
        FixedHttpPackageServer server = new FixedHttpPackageServer.Builder().
                addClientName("master").
                addClientName("client").
                setPort(SERVER_PORT).
                build();

        SimpleQueueConnection<HashMap<String, String>> inputConnection = new SimpleQueueConnection.Builder<HashMap<String, String>>().
                build();
        SimpleQueueConnection<HashMap<String, String>> outputConnection = new SimpleQueueConnection.Builder<HashMap<String, String>>().
                build();
        Map<String, Object> masterEnv = new HashMap<String, Object>();
        masterEnv.put("inputConnection", inputConnection);
        masterEnv.put("outputConnection", outputConnection);
        NetworkEnvironment<HashMap<String, String>> masterEnvironment = loadNetworkEnvironment(
                "src/test/resources/org/netwia/networks/test-sum-master.xml", masterEnv);
        StartStopService masterProcessor = NetworkUtils.createNetworkProcessor(masterEnvironment);
        
        try {
            server.start();
            masterProcessor.start();

            Scanner sc = new Scanner(System.in);
            boolean run = true;
            while (run) {
                System.out.print("Enter comma separated numbers or type exit: ");
                String input = sc.nextLine();
                if ("exit".equals(input)) {
                    run = false;
                } else {
                    List<String> numbers = Arrays.asList(input.split(","));
                    HashMap<String, String> task = new HashMap<String, String>();
                    for (int i = 0; i < numbers.size(); ++i) {
                        task.put("number" + i, numbers.get(i));
                    }
                    inputConnection.outputProduced(task);
                }
            }
            
        } finally {
            masterProcessor.stop();
            server.stop();
        }        
    }

    /**
     * Loads network environment from the specified file.
     * 
     * @param fileName file name
     * @param environment system environment
     * @return this instance
     */
    private static NetworkEnvironment<HashMap<String, String>> loadNetworkEnvironment(
            String fileName, Map<String, ? extends Object> environment) {
        Document doc = DomainUtils.createDocumentFromXmlFile(new File(fileName));
        org.w3c.dom.Node node = doc.getLastChild();
        NetworkEnvironment<HashMap<String, String>> res =
                NetworkUtils.createNetworkEnvironmentFromXml(node, new HashMap<String, String>(), environment);
        return res;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
